package com.felix.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * demo
 *
 * @author xjh
 * @version 1.0
 * @date 2019-03-04
 */
public class NewPatterns {

    private static List<Pattern> patternItems = new ArrayList<>();

    static {
        /**
         * 按照 自己想要的顺序将正则添加进列表
         */
        patternItems.add(Pattern.compile("\\d"));
        patternItems.add(Pattern.compile("\\w"));
        patternItems.add(Pattern.compile("\\s"));
        patternItems.add(Pattern.compile("\\d+"));
        patternItems.add(Pattern.compile("[a-zA-Z]+"));
        patternItems.add(Pattern.compile("\\w+"));
        patternItems.add(Pattern.compile("\\s+"));
    }

    /**
     * 遍历获得符合的正则串
     * @param con
     * @return
     */
    private static Pattern getPattern(String con){
        Pattern ptn = null;
        for (Pattern item : patternItems){
            Matcher m = item.matcher(con);
            if (m.lookingAt()){
                if (con.equals(m.group())){
                    ptn =  item;
                    break;
                }
            }
        }
        return ptn;
    }

    /**
     * 通过遍历匹配, 组装各部分的正则表达式
     * @param content
     * @return
     */
    public static String parsePattern(String content){
        List<String> tmp = split(content);
        StringBuilder sb = new StringBuilder();
        for (String s : tmp){
            //System.out.println("s: "+s);
            Pattern pth = getPattern(s);
            if (pth == null){
                sb.append(s);
            }else {
                sb.append(getPattern(s).pattern());
            }
        }
        return sb.toString();
    }

    /**
     * 分词: 简单起见只划分了字母数组和其他字符
     * @param content
     * @return
     */
    public static List<String> split(String content){
        List<String> tmp = new ArrayList<>(16);
        char[] chars = content.toCharArray();
        int tp = 0;
        for (int i=0;i<chars.length;i++){
            StringBuilder word = new StringBuilder();
            tp = getCharType(chars[i]);
            for (int j=i;j<chars.length;j++){
                int type = getCharType(chars[j]);
                if(type != tp){
                    tmp.add(word.toString());
                    i=j-1;
                    break;
                }
                word.append(chars[j]);
                if (j == chars.length-1){
                    tmp.add(word.toString());
                    i=j;
                    break;
                }
            }
        }
        return tmp;
    }

    /**
     * 给 字符 分类
     * 0 - 其他字符
     * 1 - 数字
     * 2 - 字母
     *
     * @param c
     * @return
     */
    private static int getCharType(char c){
        if(c>=48 && c<=57){
            return 1;
        }else if((c>=65 && c<=90) || (c>=97 && c<=122)){
            return 2;
        }else {
            return 0;
        }
    }

    public static void main(String[] args){
        System.out.println(isOdd(-2));
    }

    public static boolean isOdd(int a){
        return (a & 1) == 1;
    }
}
